#pragma once

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cassert>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/stat.h>
#include<sys/shm.h>
#include<fcntl.h>
#include"log.hpp"

using namespace std;

#define PATH_NAME "."

#define SIZE 4096

#define ID 066

#define FIFO_NAME "./fifo"

#define READ O_RDONLY

#define WRITE O_WRONLY

class Init
{
public:
    Init()
    {
        int n = mkfifo(FIFO_NAME, 0664);
        if(n == -1)
        {
            perror("mkfifo");
            exit(1);
        }
    }

    ~Init()
    {
        int n = unlink(FIFO_NAME);
        assert(n != -1);
    }
};


int Open(string pathname, int flags)
{
    int fd = open(pathname.c_str(), flags);
    if(fd < 0)
    {
        perror("open");
        exit(1);
    }
    return fd;
}

void Wait(int fd)
{
    log("waiting....", Notice) << endl;
    uint32_t temp = 0;
    ssize_t sz = read(fd, &temp, sizeof(temp));
    assert(sz == sizeof(temp));
}

void Signal(int fd)
{
    uint32_t temp = 1;
    ssize_t sz = write(fd, &temp, sizeof(temp));
    assert(sz == sizeof(temp));
    log("signal....", Notice) << endl;
}

void Close(int fd)
{
    close(fd);
}